﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NHibernate;

namespace Data
{
    public class BaseDal <T> where T:class 
    {
        private ISession _session = NhibernateHelper.CurrentSessionFactory.OpenSession();

        public int Insert(T model)
        {
            int id =(int) _session.Save(model);
            _session.Flush();
            return id;
        }

        public void Update(T model)
        {
            using (ITransaction ts = _session.BeginTransaction())
            {
                try
                {
                    _session.Update(model);
                    _session.Flush();
                    ts.Commit();
                }
                catch (Exception)
                {
                    ts.Rollback();
                    throw;
                }
            }
        }

        public void Delete(T model)
        {
            _session.Delete(model);
            _session.Flush();
        }

        public T Query(int id)
        {
            return _session.Get<T>(id);
        }

        
    }

}
